<?php
	require_once 'config.php';

class Cart_class
{	
	var $Message="";
	
	var $cart="";
	var $item="";
	var $price=0;
	var $addon_price=0.00;
	var $addon_color="";
	var $addon_size="";
	var $qty=0;
	var $color_price=0.00;
	var $ship_charge=0.00;
	var $total_price=0.00;
	var $contract_price=0.00;
	var $contract_colors=0;
	var $reorder;
	
	var $Buyer_id=-1;
	var $Buyer_pass="";	
	var $buyer_name="";
	var	$shipping_add="";
	var $bill_add="";
	var $buyer_phone="";
	var $buyer_email="";
	var $buyer_paypal="";
	
	var $order_id=-1;
	var $Product_Listorder='product_title';
	var $product_id;
	
	var $order_desc;
	var $bill_amount;	
	var $payment_mode;
	var $bank;	
	var $chq_dd_no;
	var $order_status;
	var $order_total;
	var $order_date;

	
	//CLASS CONSTRUCTORS.

	function __construct()//DEFAULT CONSTRUCTOR
	{
	}
	
	function __destruct()//DEFAULT CONSTRUCTOR
	{
	}
	
//**********************************	

//CLASS FUNCTIONS.

	function Set_Vars()
	{
		$_SESSION["Cart"]="";	
		$_SESSION["Buyer_id"]="";
		$_SESSION["Bsite_id"]="";
		$_SESSION["BID"]="";
		$_SESSION["ORDER_ID"]=-1;	
	}	
	
	
	function Buyer_SignUp()
	{
	
	}
	
	function Buyer_Login()
	{
		$act_conn=OpenConnection();		
		$Sql_query="SELECT * FROM BUYERS WHERE Bid=".$this->Buyer_id." AND Bpass='".$this->Buyer_pass."' AND Site_id=".$_SESSION["SITE_ID"];
		$rs=mysql_query($Sql_query,$act_conn) or die(mysql_error());
		$result=mysql_fetch_array($rs);
		if($result["Bid"]!='')
		{
			$_SESSION["Buyer_id"]=$result["Bid"];
			$_SESSION["Bsite_id"]=$result["Site_id"];
			$_SESSION["BID"]=$result["id"];			
		}
		
	}	
	
	function Buyer_Logout()
	{		
			$_SESSION["Buyer_id"]="";
			$_SESSION["Bsite_id"]="";
			$_SESSION["BID"]="";
	}	
	
	function Show_Product_Catalog()
	{	
		$act_conn=OpenConnection();				
		
		$Sql_query="SELECT * FROM SITE_PRODUCT LEFT JOIN PRODUCT_MASTER ON SITE_PRODUCT.product_id= PRODUCT_MASTER.product_id WHERE SITE_PRODUCT.site_id=".$_SESSION["SITE_ID"]." ORDER BY PRODUCT_MASTER.".$this->Product_Listorder; 
		//echo $Sql_query;
		$rs=mysql_query($Sql_query,$act_conn) or die(mysql_error());		
		echo'<table width="100%" border="1" align="center">';
		echo'<tr>';
		echo'<td colspan="2"><div align="center"></div>';
		echo'<input type="hidden" name="current_prod_id" id="current_prod_id" value="'.$this->product_id.'"/>';
		echo'<input type="hidden" name="cart_values" id="cart_values" value="'.$this->cart.'">';
		echo'<input type="button" name="btn_Viewcart" value="View Cart" onclick="javascript: viewcart();">';
		echo'<input type="submit" name="btn_Checkout" value="Checkout" <!-- onclick="javascript: return checkCartEmpty('.$_SESSION["Cart"].');" --> />';
		echo'<input type="submit" name="btn_OrderList" value="Re Orders" onclick="javascript: return checkBuyerLogin('.$_SESSION["BID"].');"/> ';
		echo'<input type="submit" name="btn_BuyerLogin" value="Buyers Login"/>';
		echo'';
		echo'</td></tr>';
		while($result=mysql_fetch_array($rs))		
		{	//echo"XXXXX".$result[0];
			$this->product_id=$result[2];
			$this->item=$result[3];
			
			echo'<tr>';
				echo'<td colspan="2"><div align="center">'.$result[3].'</div><input type="hidden" name="item'.$this->product_id.'" value="'.$result[3].'"></td>'; 
			echo'</tr>';
			echo'<tr>'; 
				echo'<td width="27%"><div align="left">'.$result[15].'</div></td>';
				echo'<td width="73%"><img width="150" height="150" src="'.$result[16].'"/></td>';
			echo'</tr>';
			echo'<tr>';
				echo'<td>price</td>';
				echo'<td><div id="div_price'.$this->product_id.'">';
				  echo $result[9];$this->price=$result[9];
				echo'</div> <input type="hidden" name="txt_price'.$this->product_id.'" id="txt_price'.$this->product_id.'" value="'.$this->price.'" /></td>';
			  echo'</tr>';
			  echo'<tr>';
				echo'<td>Addons</td>';				
				echo'<td>Select Addon <br>';
			 $this->ListAddons();
			 echo"<br>Selected Addon Price:<input type='text' name='txt_addonPrice".$this->product_id."' id='txt_addonPrice".$this->product_id."' value='".$this->addon_price."' readonly/>
			 Color:<input type='text' name='txt_SelectedColor".$this->product_id."' id='txt_SelectedColor".$this->product_id."' value='".$this->addon_color."' readonly/>
			 Size:<input type='text' name='txt_SelectedSize".$this->product_id."' id='txt_SelectedSize".$this->product_id."' value='".$this->addon_size."' readonly/>
			 </td>";
			  echo'</tr>';
			  echo'<tr>';
				echo'<td>Print Colors</td>';
				echo'<td><select name="contract_colors'.$this->product_id.'">
				  <option value="0">--</option>
				  <option value="1">1</option>
				  <option value="2">2</option>
				  <option value="3">3</option>
				  <option value="4">4</option>
				  <option value="5">5</option>
				  <option value="6">6</option>
					</select>Select no. of printing colors.<B>[OPTIONAL]</B></td>';
			  echo'</tr>';			 
				echo'<td>Shipping Charges</td>';
				echo'<td><div id="div_shipcharge'.$this->product_id.'">'.$result[8].'</div><input type="hidden" id="shipcharges'.$this->product_id.'" name="shipcharges'.$this->product_id.'" value="'.$result[8].'"/></td>';
			  echo'</tr>';
			  echo'<tr>';
				echo'<td>Quantity</td>';
				echo'<td><input type="text" name="txt_quantity'.$this->product_id.'" id="txt_quantity'.$this->product_id.'" value="'.$this->qty.'"></td>';
			  echo'</tr>';
			  echo'<tr>';
				echo'<td>Total Price </td>';
				echo'<td>$<input type="text" name="total_price'.$this->product_id.'" value="'.$this->total_price.'" readonly/><input name="btn_Calculate'.$this->product_id.'" type="submit" id="btn_Calculate'.$this->product_id.'" value="Calculate Price" onclick="javascript: return Calculate_Cart_Price('.$this->product_id.');"></td>';
			  echo'</tr>';
			  echo'<tr>';
				echo'<td>&nbsp;</td>';
				echo'<td>';
				  echo'<input name="btn_AddCart'.$this->product_id.'" type="submit" id="btn_AddCart'.$this->product_id.'" value="Add to Cart" onclick="javascript: return Calculate_Cart_Price('.$this->product_id.');">';
				  echo'<input name="btn_Buy'.$this->product_id.'" type="submit" id="Btn_Buy'.$this->product_id.'" value="Buy Now" onclick="javascript: return Calculate_Cart_Price('.$this->product_id.');">';
				  echo'<input type="button" name="btn_Viewcart" value="View Cart" onclick="javascript: viewcart();">';
				  echo'<input type="submit" name="btn_Checkout" value="Checkout" <!-- onclick="javascript: return checkCartEmpty('.$_SESSION["Cart"].');" -->/>';
				echo'</td>';
			  echo'</tr>';			
		}
		echo'</table>';
	CloseConnection($act_conn);		
	}
	
	function Calculate_Price()
	{
		$act_conn=OpenConnection();
		if($this->contract_colors!=0)			
		{			
			//CHECKING IF PROPER ADDON SIZE COLOR PRICE COMBINATION SELECTED.
			$Sql_query="SELECT addon_id FROM product_addon WHERE addon_price=".$this->addon_price." AND addon_color='".$this->addon_color."' AND addon_size='".$this->addon_size."' AND product_id=".$this->product_id;
			//echo $Sql_query;
			$res=mysql_query($Sql_query, $act_conn) or die(mysql_error());
			$result=-1;
			$result=mysql_result($res, 0);
			if($result<0)
			{
				$this->Message="Selected Invalid Addon Size. Please Check Again";
				$this->total_price=0.00;
				return;
			}
			
			//GET CONTRACT PRICING FOR COLORS.			
			$Sql_query="SELECT MIN(lot_size_m) FROM product_lot_pricing WHERE product_id=".$this->product_id." AND no_of_colors=".$this-> contract_colors;
			$res=mysql_query($Sql_query, $act_conn) or die(mysql_error());
			$result=mysql_result($res,0);
			if($result < $this->qty)
			{
				//NO CHARGE CALCULATION. WILL BE CHARGED AS SINGLE PIECES.
			}
			else
			{
				$Sql_query="SELECT MAX(lot_price) FROM product_lot_pricing WHERE product_id=".$this->product_id." AND no_of_colors=".$this->contract_colors." AND lot_size_m<=".$this->qty;	
				//echo $Sql_query;
				$res=mysql_query($Sql_query, $act_conn) or die(mysql_error());
				$contract_price=mysql_result($res,0);				
			}		
			
			//echo '*******'.$this->total_price;
		}	//IF COLORS=0 CLOSED.
		//echo"price".$this->price."shipping".$this->ship_charge."addon".$this->addon_price."contract".$this->contract_price;
			$this->total_price=($this->price+$this->ship_charge+$this->addon_price+$this->contract_price)*$this->qty;
			CloseConnection($act_conn);
	}
	
	function Add_To_Cart()
	{
		$this->cart1=$_SESSION["Cart"];
		if($this->addon_color=="") $this->addon_color="-";
		if($this->addon_size=="") $this->addon_size="-";
		
		$this->cart=$this->cart1."##".$this->product_id.'#'.$this->item.'#'.$this->addon_color.'#'.$this->addon_size.'#'.$this->contract_colors.'#'.$this->addon_price.'#'.$this->contract_price.'#'.$this->price.'#'.$this->ship_charge.'#'.$this->qty.'#'.$this->total_price;
		$_SESSION["Cart"]=$this->cart;	
		$this->Message="Item Added To Cart.";	
		echo $_SESSION["Cart"];		
	}
	
	function Remove_From_Cart($item_code)
	{
		//echo 'start'.$this->cart;
		$this->cart=$_SESSION["Cart"];
		$S_index=strpos($this->cart, '##'.$item_code);
		$L_index=strpos($this->cart,'##',($S_index+2));
		$I_length=$L_index-$S_index-1;
		$this->cart=substr($this->cart,$S_index,$I_length);
		//echo $this->cart;
	}
	
	function Create_Order_Form($order_id)
	{
	
		$act_conn=OpenConnection();
		if($order_id<0)
		{
			//CREATE ORDER FORM FORM SESSION.
			$order_desc=$_SESSION["Cart"]; 
			echo'<table width="100%" border="1">';
			  echo'<tr>';
				echo'<td colspan="10">Order Id:'.$order_id;
				echo'        Ordered On:'.date("m/d/Y").' </td>';
			  echo'</tr>';
			  echo'<tr>';
				echo'<td width="10%">Item Code </td>';
				echo'<td width="15%">Item</td>';
				echo'<td width="15%">Addon</td>';
				echo'<td width="9%">Addon_Price</td>';
				echo'<td width="11%">Printing_colors</td>';
				echo'<td width="9%">Printing Price</td>';
				echo'<td width="7%">Item Price </td>';
				echo'<td width="6%">Qty</td>';
				echo'<td width="6%">Shipping</td>';
				echo'<td width="12%">Total</td>';
			  echo'</tr>';			  

			while(strlen($order_desc)>0)
			{
				$S_Index=strpos($order_desc,'##');
				$L_Index=strpos($order,'##',$S_Index+2);
				$ItemString=substr($order_desc,($S_Index+2),($L_Index-$S_Index-3));
				$Items=split('#',$ItemString);
				echo $ItemString;			
			
			  echo'<tr>';
				echo'<td>'.$Items[0].'</td>';				
				echo'<td>'.$Items[1].'</td>';
				echo'<td>'.$Items[2].'</td>';
				echo'<td>'.$Items[3].'</td>';
				echo'<td>'.$Items[4].'</td>';
				echo'<td>'.$Items[5].'</td>';
				echo'<td>'.$Items[6].'</td>';
				echo'<td>'.$Items[7].'</td>';
				echo'<td>'.$Items[8].'</td>';
				echo'<td>'.$Items[9].'</td>';
			  echo'</tr>';
			  $newLength=strlen($order_desc)-($L_Index-1);
			  $order_desc=substr($order_desc,($L_Index-1),$newLength);
			 }			  		 
			echo'</table>';
			
		}
		else
		{			
			//CREATE ORDER FORM FROM DB.			
			$Sql_query="SELECT * FROM orderstable WHERE order_id=".$order_id;
			$res=mysql_query($res,$act_conn) or die(mysql_error());
			$result=mysql_fetch_array($res, MYSQL_ASSOC);
			echo'<table width="100%" border="1">';
			  echo'<tr>';
				echo'<td colspan="10">Order Id:'.$order_id;
				echo'Ordered On: </td>'.$result["Order_date"];
			  echo'</tr>';
			  echo'<tr>';
				echo'<td width="10%">Item Code </td>';
				echo'<td width="15%">Item</td>';
				echo'<td width="15%">Addon</td>';
				echo'<td width="9%">Addon_Price</td>';
				echo'<td width="11%">Printing_colors</td>';
				echo'<td width="9%">Printing Price</td>';
				echo'<td width="7%">Item Price </td>';
				echo'<td width="6%">Qty</td>';
				echo'<td width="6%">Shipping</td>';
				echo'<td width="12%">Total</td>';
			  echo'</tr>';			  
			$order_desc=$result["Order_desc"];
			while(strlen($order_desc)>0)
			{
				$S_Index=strpos($order_desc,'##');
				$L_Index=strpos($order,'##',$S_Index+2);
				$ItemString=substr($order_desc,($S_Index+2),($L_Index-$S_Index-3));
				$Items=split('#',$ItemString);
				//echo $ItemString;			
			
			  echo'<tr>';
				echo'<td>'.$Items[0].'</td>';				
				echo'<td>'.$Items[1].'</td>';
				echo'<td>'.$Items[2].'</td>';
				echo'<td>'.$Items[3].'</td>';
				echo'<td>'.$Items[4].'</td>';
				echo'<td>'.$Items[5].'</td>';
				echo'<td>'.$Items[6].'</td>';
				echo'<td>'.$Items[7].'</td>';
				echo'<td>'.$Items[8].'</td>';
				echo'<td>'.$Items[9].'</td>';
			  echo'</tr>';
			  $newLength=strlen($order_desc)-($L_Index-1);
			  $order_desc=substr($order_desc,($L_Index-1),$newLength);
			 }			  		 
			echo'</table>';
		}//ELSE SEC OF IF ORDER FROM DB.
		
			//SHIPPING ADDRESS SECTION.
			if($_SESSION["BID"]>0)
			{
				$Sql_query="SELECT * FROM buyers WHERE Bid=".$_SESSION["BID"]." AND site_id=".$_SESSION["Bsite_id"];
				$res=mysql_query($Sql_query, $act_conn) or die(mysql_error());
				$result=mysql_fetch_array($res, MYSQL_ASSOC);
				
				$this->buyer_name=$result["fname"]." ".$result["lname"];
				$this->shipping_add=$result["shipaddress"];
				$this->bill_add=$result["billaddress"];
				$this->buyer_phone=$result["phone"];
				$this->buyer_email=$result["email"];
				$this->buyer_paypal=$result["paypal_id"];
			}
			
		echo'<table width="100%" border="1">
		  <tr>
			<td colspan="2">Shipping Details: (You can edit provided shipping details, they will be saved for future use.) </td>
		  </tr>
		  <tr>
			<td>Name</td>
			<td>';
			echo($this->buyer_name=="")?'<input type="text" name="buyer_name" value="'.$this->buyer_name.'" >': $this->buyer_name;
			echo'</td>
		  </tr>
		  <tr>
			<td>Shipping Address </td>
			<td><textarea name="shipping_add" rows="5" cols="25">'.$this->shipping_add.'</textarea></td>
		  </tr>
		  <tr>
			<td>Billing Address</td>
			<td><textarea name="bill_add" rows="5" cols="25">'.$this->bill_add.'</textarea></td>
		  </tr>
		  <tr>
			<td>Phone No. </td>
			<td><input type="text" name="buyer_phone" value="'.$this->buyer_phone.'" /></td>
		  </tr>
		  <tr>
			<td>E mail </td>
			<td><input type="text" name="buyer_email" value="'.$this->buyer_email.'" /></td>
		  </tr>
		  <tr>
			<td>Paypal Id </td>
			<td><input type="text" name="buyer_paypal" value="'.$this->buyer_paypal.'" /></td>
		  </tr>
		  <tr>
		  	<td colspan="2"><div align="center"><input type="button" name="btn_SaveChanges" value="Save Changes" onclick="javascript: doShipChanges();"><input type="hidden" name="do_Changes" id="do_Changes" value=""></div></td>
		  </tr>
		  <tr>
		  	<td colspan="2">
				<input type="submit" name="btn_OCheckout" value="Checkout" onclick="javascript: return validateCheckOut();"/>
			</td>
		  </tr>
  		</table>';							
			
			CloseConnection($act_conn);		
	}
	
	function List_Orders($buyer_id)
	{
		$act_conn=OpenConnection();
		$Sql_query="SELECT * FROM orderstable WHERE Buyer_ID=".$buyer_id." AND Site_id=".$_SESSION["SITE_ID"];
		$res=mysql_query($Sql_query,$act_conn);
		echo'<table border="1">';
				echo'<tr>';
					echo'<td>Order No.</td>';				
					echo'<td>Ordered On</td>';
					echo'<td>Amount</td>';
					echo'<td>Shipping Address</td>';
					echo'<td>Payment Mode</td>';
					echo'<td>Bank</td>';
					echo'<td>Paypal Id</td>';
					echo'<td>Cheque/ DD No.</td>';
					echo'<td>Order Status</td>';				
				echo'</tr>';
			while($result=mysql_fetch_array($res, MYSQL_ASSOC))
			{
				echo'<tr>';
					echo'<td><input type="image" name="img_odr_frm" value="'.$result["Order_id"].'" src="images/odr.jpg" alt="Show Order Form" width="25" height="25"></td>';				
					echo'<td>'.$result["Order_date"].'</td>';
					echo'<td>'.$result["Bill_amount"].'</td>';
					echo'<td>'.$result["Shipping_add"].'</td>';
					echo'<td>'.$result["Payment_mode"].'</td>';
					echo'<td>'.$result["Bank"].'</td>';
					echo'<td>'.$result["Pay_id"].'</td>';
					echo'<td>'.$result["chq_dd_no"].'</td>';
					echo'<td>'.$result["Order_status"].'</td>';				
				echo'</tr>';
			}				
		echo'</table>';	
		CloseConnection($act_conn);	
	}
	
	function Reorder()
	{
		
	}
	
	function Check_out()
	{
	
	}
	
	function ListAddons()
	{
		$act_conn=OpenConnection();		
		$Sql_query="SELECT MAX(addon_id) FROM product_addon WHERE product_id=".$this->product_id;	
		//echo $Sql_query;
		$res=mysql_query($Sql_query, $act_conn);
		$addon_id=mysql_result($res,0);
		
		if($addon_id=="")
		{
			echo"No addons available with this product";
			return;
		}
		
		$Sql_query="SELECT DISTINCT addon_color FROM product_addon WHERE product_id=".$this->product_id;	
		//echo $Sql_query;	
		$res=mysql_query($Sql_query,$act_conn) or die(mysql_error());
		
		$color_ary=array();
		$i=0;
		while($result=mysql_fetch_array($res))
		{ 
			$color_ary[$i]=$result['addon_color'];
			$i++;
		}				
		
		$size_ary=array();
		$Sql_query="SELECT DISTINCT addon_size FROM product_addon WHERE product_id=".$this->product_id;
		$res=mysql_query($Sql_query,$act_conn) or die(mysql_error());		
		$i=0;
		while($result=mysql_fetch_array($res))
		{
			$size_ary[$i]=$result['addon_size'];
			$i++;
		}		
		echo"<b>Click an addon price from below list to add it.</b>";
		echo"<table border='1'><tr><td></td>";
		
		for($i=0; $i<count($size_ary);$i++)
		{ 
			echo'<td title="Click to choose this size" onclick="javascript: selectSize('.$this->product_id.', this)">'.$size_ary[$i].'</td>';
		}
		echo"</tr>";
		for($i=0; $i<count($color_ary); $i++)
		{ 
			echo"<tr onclick='javascript: selectColor(".$this->product_id.", this)'><td >".$color_ary[$i]."</td>";
			for($j=0; $j<count($size_ary);$j++)
			{
				$qry="SELECT addon_price FROM product_addon WHERE addon_size='".$size_ary[$j]."' AND addon_color='".$color_ary[$i]."' AND product_id=".$this->product_id;				
				$res=mysql_query($qry,$act_conn) or die(mysql_error());				
				//$result=mysql_fetch_array($res);
				$result=mysql_result($res,0);
				
				echo"<td title='Click the price to add it.' onclick=\"javascript: selectedAddonPrice(".$this->product_id.",this)
				\">".$result."</td>";
			}
			echo"</tr>";
		}
		CloseConnection($act_conn);		
		//echo"</tr>";			
		echo"</table>";		
	}
	
}
?>